package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.dto.NursingLevelPageQueryDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NursingLevelServiceImpl implements NursingLevelService {
    @Autowired
    NursingLevelMapper nursingLevelMapper;
    @Autowired
    NursingPlanMapper nursingPlanMapper;
    /**
     * 分页查询护理等级信息
     *
     * @param nursingLevelPageQueryDto
     * @return
     */
    @Override
    public PageBean<NursingLevelVo> pageQuery(NursingLevelPageQueryDto nursingLevelPageQueryDto) {
        //开启分页查询
        PageHelper.startPage(nursingLevelPageQueryDto.getPageNum(), nursingLevelPageQueryDto.getPageSize());
        Page<NursingLevelVo> page = nursingLevelMapper.pageQuery(nursingLevelPageQueryDto);
        PageBean<NursingLevelVo> pageBean = PageBean.of(page, NursingLevelVo.class);
        return pageBean;
    }

    /**
     * 插入护理等级信息
     *
     * @param nursingLevelVo
     */
    @Override
    public void insertNursingLevel(NursingLevelVo nursingLevelVo) {
        NursingLevel nursingLevel = BeanUtil.toBean(nursingLevelVo, NursingLevel.class);
        nursingLevel.setCreateTime(LocalDateTime.now());
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevel.setUpdateBy(1671403256519078138L);
        nursingLevel.setCreateBy(1671403256519078138L);
        nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 根据id查询等级信息
     *
     * @param id
     * @return
     */
    @Override
    public NursingLevelVo selectById(Long id) {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 更新护理等级信息
     *
     * @param dto
     */
    @Override
    public void updateNursingLevel(NursingLevelDto dto) {
        NursingLevel nursingLevel = BeanUtil.toBean(dto, NursingLevel.class);
        nursingLevel.setCreateTime(LocalDateTime.now());
        nursingLevel.setUpdateTime(LocalDateTime.now());
        nursingLevel.setUpdateBy(1671403256519078138L);
        nursingLevel.setCreateBy(1671403256519078138L);
        nursingLevelMapper.updateNursingLevel(nursingLevel);
    }

    /**
     * 启用禁用
     *
     * @param id
     * @param status
     */
    @Override
    public void enableOrDisable(Long id, Integer status) {
        nursingLevelMapper.updateStatus(id,status);
    }

    /**
     * 删除护理等级信息
     *
     * @param id
     */
    @Override
    public void delete(Long id) {
        NursingLevelVo nursingLevelVo = nursingLevelMapper.selectById(id);
        if(nursingLevelVo.getStatus()==null){
            throw new RuntimeException("护理状态异常，禁止删除");
        }
        if (nursingLevelVo.getStatus()==1){
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        nursingLevelMapper.delete(id);
    }

    /**
     * 查询所有护理等级
     *
     * @return
     */
    @Override
    public List<NursingLevelVo> getAllList() {
        return nursingLevelMapper.getAllList();
    }


}


